 var billId = GetQueryString('billId');
     setInfo();
  	var brandId = null;
  	var seriesId = null;
  	var modelId = null;
    var r;
  	function initBrand(){

  		$.ajax({
	      type: "get",      //data 传送数据类型。post 传递
	      dataType: 'json',  // 返回数据的数据类型json
	      url: defaultUrl+"/controller/getGroupBrand",  // yii 控制器/方法
	      cache: false,      
	      // data: {tel: tel},  //传送的数据
	      error:function(){
	         alert("数据传输错误");
	      },success: function (data) {
	         console.log(data);
	         if (data.code != 1) {
	         	return false
	         }

	        var resArr =  data.result;
	        var wordsHtml = "";
	        var brandHtml = "";
	        for (var i = 0; i < resArr.length; i++) {
	        	wordsHtml += '<span>'+resArr[i].initial+'</span>';
	        	var brandListHtml = '<ul class="brand-list">';
	        	var brandArr = resArr[i].brandInfo;
	        	for (var j = 0; j < brandArr.length; j++) {
	        		brandArr[j].brandLogo
	        		brandListHtml+='<li class="on" id="'+brandArr[j].brandId+'" onclick="showCars(\''+brandArr[j].brandName+'\',id)"><i style="background-image: url('+brandArr[j].brandLogo+');"></i><span>'+brandArr[j].brandName+'</span></li>'

	        	}
	        	brandListHtml+='</ul>';
	        	brandHtml+=brandListHtml;
	        }
	        $('.brand-box').html(brandHtml);
	        $('.words').html(wordsHtml);
	        $('.words span').eq(0).addClass('on');
	        $('.brand-list').eq(0).show();


	        $('.words span').click(function(){
  		  		$('.words span').removeClass('on');
  		  		$(this).addClass('on');
  		  		$('.brand-list').hide();
  		  		$('.brand-list').eq($(this).index()).show();
  		  	});

          // setBrand();
          


	      }
  		});
  		$.ajax({
  	      type: "get",      //data 传送数据类型。post 传递
  	      dataType: 'json',  // 返回数据的数据类型json
  	      url: defaultUrl+"/controller/getMarketList",  // yii 控制器/方法
  	      cache: false,      
  	      data: {'marketId': marketId},  //传送的数据
  	      error:function(){
  	         alert("数据传输错误");
  	      },success: function (data) {
  	      	console.log(data);
  	         if (data.code != 1) {
  	         	return false
  	         }
  	         var resultArr = data.result;
  	         var marketList = "";
  	         for (var i = 0; i < resultArr.length; i++) {
  	         	marketList += '<option value="'+resultArr[i].marketId+'">'+resultArr[i].marketName+'</option>'
  	         }
  	         $('#marketSelect').html(marketList);
             $('select[name="marketId"]').find("option[value='"+r.marketId+"']").attr("selected",true);

             findShop(r.marketId);


             
  	      }
  		});


  		$.ajax({
  	      type: "get",      //data 传送数据类型。post 传递
  	      dataType: 'json',  // 返回数据的数据类型json
  	      url: defaultUrl+"/controller/area/province",  // yii 控制器/方法
  	      cache: false,      
  	      // data: {tel: tel},  //传送的数据
  	      error:function(){
  	         alert("数据传输错误");
  	      },success: function (data) {
  	      	console.log(data);
  	         if (data.code != 1) {
  	         	return false
  	         }
  	         var resultArr = data.result;
  	         var provinceList = '<option value="">请选择</option>';
  	         for (var i = 0; i < resultArr.length; i++) {
  	         	provinceList += '<option value="'+resultArr[i].provinceId+'">'+resultArr[i].provinceName+'</option>'
  	         }
  	         $('#provinceSelect').html(provinceList);

             $('select[name="provinceId"]').find("option[value='"+r.provinceId+"']").attr("selected",true);
             findCity(r.provinceId);
             
  	      }
  		});

  		$.ajax({
  	      type: "get",      //data 传送数据类型。post 传递
  	      dataType: 'json',  // 返回数据的数据类型json
  	      url: defaultUrl+"/controller/getCarTypeList",  // yii 控制器/方法
  	      cache: false,      
  	      // data: {tel: tel},  //传送的数据
  	      error:function(){
  	         alert("数据传输错误");
  	      },success: function (data) {
  	      	console.log(data);
  	         if (data.code != 1) {
  	         	return false
  	         }
  	         var resultArr = data.result;
  	         var carTypeList = "";
  	         for (var i = 0; i < resultArr.length; i++) {
  	         	carTypeList += '<option value="'+resultArr[i].carTypeId+'">'+resultArr[i].carTypeName+'</option>'
  	         }
  	         $('#carTypeSelect').html(carTypeList);
             $('select[name="carTypeId"]').find("option[value='"+r.carTypeId+"']").attr("selected",true);

  	      }
  		});

  	}
  	initBrand();
    






    //回填数据
    function setInfo(){
      console.log(billId);
      $.ajax({
          type: "get",      //data 传送数据类型。post 传递
          dataType: 'json',  // 返回数据的数据类型json
          url: defaultUrl+"/controller/getSaleBillById",  // yii 控制器/方法
          cache: false,
          async: false,      
          data: {'billId': billId,'marketId':marketId},  //传送的数据
          error:function(){
             alert("数据传输错误");
          },success: function (data) {
            console.log(data);
             if (data.code != 1) {
              return false
             }
             r = data.result;
             $('input[name="billId"]').val(r.billId);
             $('input[name="billDate"]').val(setDate(r.billDate));
             $('input[name="billCode"]').val(r.billCode);
             $('input[name="billNo"]').val(r.billNo);
             $('input[name="billPrice"]').val(r.billPrice);
             $('input[name="billCarMark"]').val(r.billCarMark);
             $('select[name="billCarPlateHead"]').find("option[value='"+r.billCarPlateHead+"']").attr("selected",true);
             $('select[name="billCarPlateMiddle"]').find("option[value='"+r.billCarPlateMiddle+"']").attr("selected",true);
             $('input[name="billCarPlateCode"]').val(r.billCarPlateCode);
             $('input[name="billCarEngineNo"]').val(r.billCarEngineNo);
             $('input[name="billCarVin"]').val(r.billCarVin);
             $('input[name="billCarName"]').val(r.billCarName);
             $('input[name="billBuyerName"]').val(r.billBuyerName);
             $('input[name="billBuyerNo"]').val(r.billBuyerNo);
             $('input[name="billBuyerAddress"]').val(r.billBuyerAddress);
             $('input[name="billBuyerMobile"]').val(r.billBuyerMobile);
             $('input[name="billSellerName"]').val(r.billSellerName);
             $('input[name="billSellerNo"]').val(r.billSellerNo);
             $('input[name="billSellerAddress"]').val(r.billSellerAddress);
             $('input[name="billSellerMobile"]').val(r.billSellerMobile);
             $('input[name="billDMV"]').val(r.billDMV);
             $('textarea[name="billRemark"]').val(r.billRemark);
             $('select[name="billCarColor"]').find("option[value='"+r.billCarColor+"']").attr("selected",true);
             $('input[name="brandName"]').val(r.model.brandName+r.model.seriesName+r.model.modelName);

          }
      });
    }


  	$('#marketSelect').change(function(){
  		console.log($(this).val());
  		$.ajax({
	      type: "get",      //data 传送数据类型。post 传递
	      dataType: 'json',  // 返回数据的数据类型json
	      url: defaultUrl+"/controller/getShopList",  // yii 控制器/方法
	      cache: false,      
	      data: {'marketId': $(this).val()},  //传送的数据
	      error:function(){
	         alert("数据传输错误");
	      },success: function (data) {
	      	console.log(data);
	         if (data.code != 1) {
	         	return false
	         }
	         var resultArr = data.result;
	         var shopList = '<option value="">请选择</option>';
	         for (var i = 0; i < resultArr.length; i++) {
	         	shopList += '<option value="'+resultArr[i].shopId+'">'+resultArr[i].shopName+'</option>'
	         }
	         $('#shopSelect').html(shopList);
	      }
		});

  	});
    function findShop(marketId){
      $.ajax({
        type: "get",      //data 传送数据类型。post 传递
        dataType: 'json',  // 返回数据的数据类型json
        url: defaultUrl+"/controller/getShopList",  // yii 控制器/方法
        cache: false,      
        data: {'marketId': marketId},  //传送的数据
        error:function(){
           alert("数据传输错误");
        },success: function (data) {
          console.log(data);
           if (data.code != 1) {
            return false
           }
           var resultArr = data.result;
           var shopList = "";
           for (var i = 0; i < resultArr.length; i++) {
            shopList += '<option value="'+resultArr[i].shopId+'">'+resultArr[i].shopName+'</option>'
           }
           $('#shopSelect').html(shopList);
           console.log(r.shopId);
           $('select[name="shopId"]').find("option[value='"+r.shopId+"']").attr("selected",true);
        }
      });
    }
    function findCity(provinceId){
      $.ajax({
          type: "get",      //data 传送数据类型。post 传递
          dataType: 'json',  // 返回数据的数据类型json
          url: defaultUrl+"/controller/area/city",  // yii 控制器/方法
          cache: false,      
          data: {'provinceId': provinceId},  //传送的数据
          error:function(){
             alert("数据传输错误");
          },success: function (data) {
            console.log(data);
             if (data.code != 1) {
              return false
             }
             var resultArr = data.result;
             var cityList = "";
             for (var i = 0; i < resultArr.length; i++) {
              cityList += '<option value="'+resultArr[i].cityId+'">'+resultArr[i].cityName+'</option>'
             }
             $('#citySelect').html(cityList);
             $('select[name="cityId"]').find("option[value='"+r.cityId+"']").attr("selected",true);
          }
      });
    }
  	$('#provinceSelect').change(function(){
  		console.log($(this).val());
  		$.ajax({
	      type: "get",      //data 传送数据类型。post 传递
	      dataType: 'json',  // 返回数据的数据类型json
	      url: defaultUrl+"/controller/area/city",  // yii 控制器/方法
	      cache: false,      
	      data: {'provinceId': $(this).val()},  //传送的数据
	      error:function(){
	         alert("数据传输错误");
	      },success: function (data) {
	      	console.log(data);
	         if (data.code != 1) {
	         	return false
	         }
	         var resultArr = data.result;
	         var cityList = "";
	         for (var i = 0; i < resultArr.length; i++) {
	         	cityList += '<option value="'+resultArr[i].cityId+'">'+resultArr[i].cityName+'</option>'
	         }
	         $('#citySelect').html(cityList);
           $('select[name="cityId"]').find("option[value='"+r.cityId+"']").attr("selected",true);
	      }
		});

  	})













  	function showCars(name,id){
      console.log('hahah'+name+id)
  		brandId = id;
  		$('.pick-box').append('<span class="cars-pick brand-pick"><span>'+name+'</span><i class="delete"></i></span>');
  		$.ajax({
	      type: "get",      //data 传送数据类型。post 传递
	      dataType: 'json',  // 返回数据的数据类型json
	      url: defaultUrl+"/controller/getSeriesList",  // yii 控制器/方法
	      cache: false,      
	      data: {'brandId': id},  //传送的数据
	      error:function(){
	         alert("数据传输错误");
	      },success: function (data) {
	      	console.log(data);
	      	if (data.code != 1) {
	         	return false
	        }

	      	var resultArr = data.result;
	      	var seriesHtml = "";
	      	for (var i = 0; i < resultArr.length; i++) {
	      		seriesHtml += '<span class="series-list" id="'+resultArr[i].seriesId+'" onclick="showModel(\''+resultArr[i].seriesName+'\',id)">'+resultArr[i].seriesName+'</span>';
	      	}

	      	$('.cars .title span').html('选择车系');

	      	$('.car').html(seriesHtml);
	      	$('.brand').hide();
	      	$('.car').show();

	      }
	  });
  	}

  	function showModel(name,id){
  		console.log(id);
  		seriesId = id;
  		$('.pick-box').append('<span class="cars-pick series-pick"><span>'+name+'</span><i class="delete"></i></span>');
  		$.ajax({
	      type: "get",      //data 传送数据类型。post 传递
	      dataType: 'json',  // 返回数据的数据类型json
	      url: defaultUrl+"/controller/getModelList",  // yii 控制器/方法
	      cache: false,      
	      data: {'seriesId': id},  //传送的数据
	      error:function(){
	         alert("数据传输错误");
	      },success: function (data) {
	      	console.log(data);
	      	if (data.code != 1) {
	         	return false
	        }

	      	var resultArr = data.result;
	      	var modelHtml = "";
	      	for (var i = 0; i < resultArr.length; i++) {
	      		modelHtml += '<span class="model-list" id="'+resultArr[i].modelId+'" onclick="selectModel(\''+resultArr[i].modelName+'\',id)">'+resultArr[i].modelName+'</span>';
	      	}
	      	$('.cars .title span').html('选择车型');
	      	$('.model').html(modelHtml);
	      	$('.car').hide();
	      	$('.model').show();

	      }
	  });
  	}



    


  	function selectModel(name,id){
  		console.log(id);
  		modelId = id;
  		if ($('.cars-pick').length >=3) {
  			$('.model-pick span').html(name);
  		}else{
  			$('.pick-box').append('<span class="cars-pick model-pick"><span>'+name+'</span><i class="delete"></i></span>');
  		}
  		
  	}
  	$(document).on("click",".brand-pick .delete",function(){  
    	$('.brand-pick').remove();
    	$('.series-pick').remove();
    	$('.model-pick').remove();
    	$('.car').hide();
    	$('.model').hide();
    	$('.brand').show();
    	$('.cars .title span').html('选择品牌');
    });
  	$(document).on("click",".series-pick .delete",function(){  
    	// $('.brand-pick').remove();
    	$('.series-pick').remove();
    	$('.model-pick').remove();
    	$('.car').show();
    	$('.model').hide();
    	$('.brand').hide();
    	$('.cars .title span').html('选择车系');
    });
    $(document).on("click",".model-pick .delete",function(){  
    	// $('.brand-pick').remove();
    	// $('.series-pick').remove();
    	$('.model-pick').remove();
    	$('.car').hide();
    	$('.model').show();
    	$('.brand').hide();
    });
    $('.brand-btn').click(function(){
    	$('.cars-bg').hide();
    	$('.brand-input').val($('.brand-pick span').html()+$('.series-pick span').html()+$('.model-pick span').html());
    	$('.brandId-input').val(brandId);
    	$('.seriesId-input').val(seriesId);
    	$('.modelId-input').val(modelId);
    	// alert(modelId);
    });
    $('.brand-input').click(function(){
    	$('.cars-bg').show();
    });
    $('.cars-close').click(function(){
    	$('.cars-bg').hide();
    });

    function setDate(stamp){
      Date.prototype.toLocaleString = function() {
        var month = this.getMonth() + 1;
        var strDate = this.getDate();

        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }

        
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        return this.getFullYear() + "-" + month + "-" + strDate;
      };
      var unixTimestamp = new Date(stamp) ;
      console.log(unixTimestamp.toLocaleString());
       return commonTime = unixTimestamp.toLocaleString();
       // alert(commonTime);
    }

    function setBrand(){
      showCars(r.brand.brandName,r.brandId);
      showModel(r.series.seriesName,r.seriesId);
      selectModel(r.model.modelName,r.modelId)
    }




    function sendForm(){
      console.log('进来了'+$('form').serialize())
      $.ajax({
          url: defaultUrl+"/controller/updateSaleBill",
          data:$('form').serialize(),
          dataType:"json",
          error:function(data){
              console.log(data);
          },
          success:function(data){
              console.log(data);
              if (data.code != 1) {
                alert(data.msg);
                return false
              }

              window.history.go(-1);
          }
      });  
      // return false;    
    }

    function cancel(){
      window.history.go(-1);
    }

